package com.meihua.baoleyi.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.meihua.baoleyi.R;


/**
 * Author  :liming
 * Created : 2017/10/20 16:25
 * Email   : maxpanda0206@gmail.com
 * Describe:  性别选择 pop
 */

public class Pop_Sex extends PopupWindow {

    private View mMenuView;
    private OnItemClickListener listener;
    /**
     * @param context
     */
    public Pop_Sex(final Activity context, OnItemClickListener listener) {
        super(context);
        this.listener=listener;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_sex, null);
        initView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        backgroundAlpha(context,0.6f);
        this.setContentView(mMenuView);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(context,1f);
            }
        });
    }

    private void initView(View contentView) {
        TextView boy= (TextView) contentView.findViewById(R.id.boy);
        boy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClickListener(0);
                dismiss();
            }
        });
        TextView girl= (TextView) contentView.findViewById(R.id.girl);
        girl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClickListener(1);
                dismiss();
            }
        });
        TextView cancel= (TextView) contentView.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
    public interface  OnItemClickListener{
        void onItemClickListener(int position);
    }

}
